package com.zlx.exchange.order.config;

import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.core.*;

import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;

import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
@Log4j2
public class RabbitMQConfig {

    @Bean
    public MessageConverter messageConverter() {
        return new Jackson2JsonMessageConverter();
    }

    @Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory factory) {
        RabbitTemplate rabbitTemplate = new RabbitTemplate(factory);
        rabbitTemplate.setMessageConverter(messageConverter());
        rabbitTemplate.setMandatory(true);

        rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
            @Override
            public void confirm(CorrelationData correlationData, boolean b, String s) {
                log.debug("------------confirm信息----------------");
                log.debug("confirm:correlationData-->{}", correlationData);
                log.debug("confirm:ack-->{}", b);
                log.debug("confirm:返回信息-->{}", s);
            }
        });

        rabbitTemplate.setReturnsCallback(new RabbitTemplate.ReturnsCallback() {
            @Override
            public void returnedMessage(ReturnedMessage returnedMessage) {
                log.debug("------------return信息----------------");
                log.debug("return :RoutingKey-->{}", returnedMessage.getRoutingKey());
                log.debug("return :exchange-->{}", returnedMessage.getExchange());
                log.debug("return: replyCode-->{}", returnedMessage.getReplyCode());
                log.debug("return: replyText-->{}", returnedMessage.getReplyText());
            }
        });


        return rabbitTemplate;
    }

    /**
     * 延迟队列
     */
    public static final String DELAY_QUEUE = "delay_queue";

    /**
     * 延迟交换机
     */
    public static final String DELAY_EXCHANGE = "delay_exchange";
    /**
     * 延迟routing_key
     */
    public static final String DELAY_KEY = "delay_key";

    /**
     * 延迟交换机
     */
    @Bean
    public CustomExchange delayExchange() {
        //设置延迟队列的属性参数
        Map<String, Object> args = new HashMap<>();
        args.put("x-delayed-type", "direct");
        return new CustomExchange(DELAY_EXCHANGE, "x-delayed-message", true, false, args);
    }

    /**
     * 延迟队列
     *
     * @return
     */
    @Bean
    public Queue delayOrderQueue() {
        return new Queue(DELAY_QUEUE, true);
    }

    /**
     * 延迟队列绑定
     *
     * @return
     */
    @Bean
    public Binding delayOrderBinding() {
        return BindingBuilder.bind(delayOrderQueue()).to(delayExchange()).
                with(DELAY_KEY).noargs();
    }
}
